package com.hunttown.mes.impl;

import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.domain.AnalysisVisualCatalogDTO;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.api.AnalysisVisualCatalogRpcService;
import com.hunttown.mes.rpc.domain.query.AnalysisVisualCatalogDTOQuery;
import com.hunttown.mes.service.AnalysisVisualCatalogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.hunttown.mes.convert.AnalysisVisualCatalogConverts.convert;

/**
 * created by wangjunfu on 2018-12-12 18:11:36
 */
@Service
public class AnalysisVisualCatalogRpcServiceImpl implements AnalysisVisualCatalogRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisVisualCatalogRpcServiceImpl.class);

    private final AnalysisVisualCatalogService analysisVisualCatalogService;

    @Autowired
    public AnalysisVisualCatalogRpcServiceImpl(AnalysisVisualCatalogService analysisVisualCatalogService) {
        this.analysisVisualCatalogService = analysisVisualCatalogService;
    }

    @Override
    public AnalysisVisualCatalogDTO insert(AnalysisVisualCatalogDTO analysisVisualCatalogDTO) {
        try {
            return convert(analysisVisualCatalogService.insert(convert(analysisVisualCatalogDTO)));
        } catch (Exception e) {
            logger.error("AnalysisVisualCatalog create failed.", e);
            throw new RuntimeException("AnalysisVisualCatalog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisVisualCatalogDTO analysisVisualCatalogDTO) {
        try {
            return analysisVisualCatalogService.updateInfoById(convert(analysisVisualCatalogDTO));
        } catch (Exception e) {
            logger.error("AnalysisVisualCatalog update by id failed.", e);
            throw new RuntimeException("AnalysisVisualCatalog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisVisualCatalogDTOQuery analysisVisualCatalogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisVisualCatalogDTOQuery.getW_id() != null && analysisVisualCatalogDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisVisualCatalogDTOQuery.getW_id());
            }

            if (analysisVisualCatalogDTOQuery.getIds() != null && !analysisVisualCatalogDTOQuery.getIds().equals("")) {
                query.put("ids", analysisVisualCatalogDTOQuery.getIds());
            }

            if (analysisVisualCatalogDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", analysisVisualCatalogDTOQuery.getStopFlag());
            }

            if (analysisVisualCatalogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisVisualCatalogDTOQuery.getDeleteFlag());
            }

            return analysisVisualCatalogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisVisualCatalog update by query failed.", e);
            throw new RuntimeException("AnalysisVisualCatalog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisVisualCatalogDTO selectById(Integer id) {
        try {
            return convert(analysisVisualCatalogService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisVisualCatalog select by id failed.", e);
            throw new RuntimeException("AnalysisVisualCatalog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisVisualCatalogDTO selectObjByQuery(AnalysisVisualCatalogDTOQuery analysisVisualCatalogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisVisualCatalogDTOQuery.getModelType() != null) {
                query.put("modelType", analysisVisualCatalogDTOQuery.getModelType());
            }

            if (analysisVisualCatalogDTOQuery.getId() != null) {
                query.put("id", analysisVisualCatalogDTOQuery.getId());
            }

            if (analysisVisualCatalogDTOQuery.getParentId() != null) {
                query.put("parentId", analysisVisualCatalogDTOQuery.getParentId());
            }

            if (analysisVisualCatalogDTOQuery.getEntId() != null) {
                query.put("entId", analysisVisualCatalogDTOQuery.getEntId());
            }

            if (analysisVisualCatalogDTOQuery.getCatalogName() != null && !analysisVisualCatalogDTOQuery.getCatalogName().equals("")) {
                query.put("catalogName", analysisVisualCatalogDTOQuery.getCatalogName());
            }

            if (analysisVisualCatalogDTOQuery.getCatalogNameEn() != null && !analysisVisualCatalogDTOQuery.getCatalogNameEn().equals("")) {
                query.put("catalogNameEn", analysisVisualCatalogDTOQuery.getCatalogNameEn());
            }

            if (analysisVisualCatalogDTOQuery.getLikeName() != null && !analysisVisualCatalogDTOQuery.getLikeName().equals("")) {
                query.put("likeName", analysisVisualCatalogDTOQuery.getLikeName());
            }

            if (analysisVisualCatalogDTOQuery.getOrderBy() != null && analysisVisualCatalogDTOQuery.getOrderBy().length() > 0) {
                query.put("orderBy", analysisVisualCatalogDTOQuery.getOrderBy());
            }

            if (analysisVisualCatalogDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", analysisVisualCatalogDTOQuery.getStopFlag());
            }

            if (analysisVisualCatalogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisVisualCatalogDTOQuery.getDeleteFlag());
            }

            return convert(analysisVisualCatalogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisVisualCatalog select obj by query failed.", e);
            throw new RuntimeException("AnalysisVisualCatalog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisVisualCatalogDTO> getListForPage(AnalysisVisualCatalogDTOQuery analysisVisualCatalogDTOQuery) {
        Query query = Query.page(analysisVisualCatalogDTOQuery.getPn(), analysisVisualCatalogDTOQuery.getPageSize());

        if (analysisVisualCatalogDTOQuery.getModelType() != null && analysisVisualCatalogDTOQuery.getModelType() != -9999) {
            query.put("modelType", analysisVisualCatalogDTOQuery.getModelType());
        }

        if (analysisVisualCatalogDTOQuery.getModelTypes() != null && !analysisVisualCatalogDTOQuery.getModelTypes().equals("")) {
            query.put("modelTypes", analysisVisualCatalogDTOQuery.getModelTypes());
        }

        if (analysisVisualCatalogDTOQuery.getId() != null) {
            query.put("id", analysisVisualCatalogDTOQuery.getId());
        }

        if (analysisVisualCatalogDTOQuery.getParentId() != null) {
            query.put("parentId", analysisVisualCatalogDTOQuery.getParentId());
        }

        if (analysisVisualCatalogDTOQuery.getIsWeb() != null) {
            query.put("isWeb", analysisVisualCatalogDTOQuery.getIsWeb());
        }

        if (analysisVisualCatalogDTOQuery.getIsWebCate() != null) {
            query.put("isWebCate", analysisVisualCatalogDTOQuery.getIsWebCate());
        }

        if (analysisVisualCatalogDTOQuery.getCatalogName() != null && !analysisVisualCatalogDTOQuery.getCatalogName().equals("")) {
            query.put("catalogName", analysisVisualCatalogDTOQuery.getCatalogName());
        }

        if (analysisVisualCatalogDTOQuery.getCatalogNameEn() != null && !analysisVisualCatalogDTOQuery.getCatalogNameEn().equals("")) {
            query.put("catalogNameEn", analysisVisualCatalogDTOQuery.getCatalogNameEn());
        }

        if (analysisVisualCatalogDTOQuery.getIds() != null && !analysisVisualCatalogDTOQuery.getIds().equals("")) {
            query.put("ids", analysisVisualCatalogDTOQuery.getIds());
        }

        if (analysisVisualCatalogDTOQuery.getLikeName() != null && !analysisVisualCatalogDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisVisualCatalogDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(analysisVisualCatalogDTOQuery.getDescription())) {
            query.put("description", analysisVisualCatalogDTOQuery.getDescription());
        }

        if (analysisVisualCatalogDTOQuery.getOrderBy() != null && analysisVisualCatalogDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisVisualCatalogDTOQuery.getOrderBy());
        }

        if (analysisVisualCatalogDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", analysisVisualCatalogDTOQuery.getStopFlag());
        }

        if (analysisVisualCatalogDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", analysisVisualCatalogDTOQuery.getDeleteFlag());
        }

        List<AnalysisVisualCatalogDTO> list = convert(analysisVisualCatalogService.selectByQuery(query));
        Page<AnalysisVisualCatalogDTO> page = Page.from(analysisVisualCatalogDTOQuery.getPn(), analysisVisualCatalogDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisVisualCatalogService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisVisualCatalogService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisVisualCatalog delete failed.", e);
            throw new RuntimeException("AnalysisVisualCatalog delete failed, Please order your code.", e);
        }
    }

    @Override
    public List<Map<String, String>> selectSimilarCatalog(String similar, Integer modelType) {
        try {
            Query query = Query.page(1, 20);

            if (StringUtils.isNotBlank(similar)) {
                query.put("likeName", similar.trim());
            }

            if (modelType != null && modelType > 0) {
                query.put("modelType", modelType);
            }

            return analysisVisualCatalogService.selectSimilarCatalog(query);

        } catch (Exception e) {
            logger.error("AnalysisVisualCatalog select similar user failed.", e);
            throw new RuntimeException("AnalysisVisualCatalog similar user failed, Please order your code.", e);
        }
    }
}